package com.fsp.blog.admin.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fsp.blog.admin.dao.PermissionMapper;
import com.fsp.blog.admin.pojo.Permission;
import com.fsp.blog.admin.vo.PageResult;
import com.fsp.blog.admin.vo.Result;
import com.fsp.blog.admin.vo.params.PageParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author fushengping
 * @className PermissionService
 * @description
 * @date 2021/11/23 23:35
 */
@Service
public class PermissionService {

    @Resource
    private PermissionMapper permissionMapper;
    /**
     * 分页查询权限信息
     * @param pageParam
     * @return
     */
    public Result listPermission(PageParam pageParam) {
        //放入分页查询条件，当前页，页数据量
        Page<Permission> page = new Page<>(pageParam.getCurrentPage(),pageParam.getPageSize());
        LambdaQueryWrapper<Permission> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getQueryString())) {  //可做权限名搜索查询
            queryWrapper.eq(Permission::getName,pageParam.getQueryString());//查询姓名是否相等
        }
        //分页查询结果
        Page<Permission> permissionPage = permissionMapper.selectPage(page, queryWrapper);
        //返回给前端数据
        PageResult<Permission> pageResult = new PageResult<>();
        pageResult.setList(permissionPage.getRecords()); //选中当前页面数据
        pageResult.setTotal(permissionPage.getTotal()); //数据总数
        return Result.success(pageResult);
    }

    /**
     * 权限添加
     * @param permission
     * @return
     */
    public Result add(Permission permission) {
        this.permissionMapper.insert(permission);
        return Result.success(null);
    }

    /**
     * 权限信息修改
     * @param permission
     * @return
     */
    public Result update(Permission permission) {
        this.permissionMapper.updateById(permission);
        return Result.success(null);
    }

    /**
     * 根据权限id删除权限信息
     * @param id
     * @return
     */
    public Result delete(Long id) {
        this.permissionMapper.deleteById(id);
        return Result.success(null);
    }
}
